package me.zhengjie.utils;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class JsonUtil {
    private static ObjectMapper uniqueObjectMapper;

    public JsonUtil() {

    }

    public static ObjectMapper objectMapper() {
        if (uniqueObjectMapper == null) {
            throw new IllegalArgumentException("必须先提供默认的序列化器");
        } else {
            return uniqueObjectMapper;
        }
    }

    public static void setDefault(ObjectMapper objectMapper) {
        objectMapper.findAndRegisterModules();
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        objectMapper.enable(new Feature[]{Feature.ALLOW_UNQUOTED_FIELD_NAMES});
        objectMapper.enable(new Feature[]{Feature.ALLOW_SINGLE_QUOTES});
        objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
        objectMapper.setSerializationInclusion(Include.NON_ABSENT);
        uniqueObjectMapper = objectMapper;
    }

    public static <T> String toJson(T t) {
        try {
            return uniqueObjectMapper.writeValueAsString(t);
        } catch (JsonProcessingException var2) {
            throw new RuntimeException(var2.getMessage(), var2);
        }
    }

    public static <T> String toJsonPretty(T t) {
        try {
            return uniqueObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(t);
        } catch (JsonProcessingException var2) {
            throw new RuntimeException(var2.getMessage(), var2);
        }
    }

    public static <T> T parse(String json, Class<T> clazz) {
        try {
            return uniqueObjectMapper.readValue(json, clazz);
        } catch (IOException var3) {
            throw new RuntimeException(var3.getMessage(), var3);
        }
    }

    public static <T> T parse(String json, Class<T> clazz, T defaultValue) {
        try {
            return uniqueObjectMapper.readValue(json, clazz);
        } catch (IOException var4) {
            return defaultValue;
        }
    }

    public static <T> List<T> parse2List(String json, Class<T> clazz) {
        CollectionType type = uniqueObjectMapper.getTypeFactory().constructCollectionType(List.class, clazz);

        try {
            return (List) uniqueObjectMapper.readValue(json, type);
        } catch (IOException var4) {
            throw new RuntimeException(var4.getMessage(), var4);
        }
    }

    public static <K, V> Map<K, V> toMap(String json, Class<K> kClass, Class<V> vClass) {
        try {
            return uniqueObjectMapper.readValue(json, uniqueObjectMapper.getTypeFactory().constructMapType(Map.class, kClass, vClass));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    static {
        setDefault(new ObjectMapper());
    }
}
